package lsl.leetCode.year2023.test0130;

import java.util.HashMap;

/**
 * @author Zed
 * @description
 * @date 2023/1/30 18:52
 */
public class test383 {
    public static void main(String[] args) {
        System.out.println(canConstruct("abab", "aabb"));
    }

    public static boolean canConstruct(String ransomNote, String magazine) {
        if (ransomNote.length() > magazine.length()) {
            return false;
        }
        char[] rCharArray = ransomNote.toCharArray();
        char[] mCharArray = magazine.toCharArray();
        HashMap<Character, Integer> map = new HashMap<>();
        for (int i = 0; i < mCharArray.length; i++) {
            map.put(mCharArray[i], map.getOrDefault(mCharArray[i], 0) + 1);
        }
        for (int i = 0; i < rCharArray.length; i++) {
            if (map.get(rCharArray[i]) == null) {
                return false;
            }
            Integer count = map.get(rCharArray[i]);
            if (count - 1 < 0) {
                return false;
            }
            map.put(rCharArray[i], count - 1);
        }
        return true;
    }
}
